import tkinter as tk
from ...shoudongfasong.ui_demo_toplevel import ZCAN_Demo_Toplevel
from ....utils.loggers import ic_debugger
from tkinter import messagebox
import time
from ....zlgcan.zlgcan import *
import json

from ....utils.types import TestData_Type, Uds_Request_Type, UnitTestConfig_Type, Can_TestDataStucture, UnitTestDataStructure
from ..models.modle_base import FunctestTemplate_Uds

MAX_RCV_NUM = 10


class ModelTab_Frame(tk.Frame):
    def __init__(self, master, masterUI, parentWin):
        super().__init__(master=master)
        self.masterUI = masterUI
        self.parentWin = parentWin

        self.modelTabInit()

    def modelTabInit(self):
        self.modelTabDataInit()
        # DOC 先更新再ViewInit注册，有先后顺序要求
        self.modelTabViewInit()
        self.modelTab_btn_function_update()

    def modelTabDataInit(self):
        self.modelTab_MODEL_POS_LIST = []
        self.modelTab_MODEL_BTN_DICT = {}
        self.modelTab_MODEL_FUN_DICT = {}
        self.modelTab_MODEL_NAME_DICT = {}
        MAX_ROW = 8
        MAX_COLUMN = 8
        for i in range(MAX_ROW):
            for j in range(MAX_COLUMN):
                position = f"{i}{j}"
                self.modelTab_MODEL_POS_LIST.append(position)

        def default_model_pos_btn_click(i, j):
            return lambda: messagebox.showinfo("提示", f"model R{i}C{j}未定义")
        for pos in self.modelTab_MODEL_POS_LIST:
            self.modelTab_MODEL_FUN_DICT[pos] = default_model_pos_btn_click
            self.modelTab_MODEL_NAME_DICT[pos] = f'model_{pos}'

    def modelTab_btn_function_update(self):
        # DOC 针对每个位置的按钮，定义model_function_update_{pos},格式的函数
        # 这里自动更新已经定义的函数，否则就用默认的函数
        for pos in self.modelTab_MODEL_POS_LIST:
            update_function = getattr(
                self, f'model_function_update_{pos}', None)
            if update_function:
                update_function()

    def model_function_update_00(self):
        pos = '00'

        def wrapper():
            messagebox.showinfo('提示', 'model00-update......')
        self.modelTab_MODEL_FUN_DICT[pos] = wrapper
        self.modelTab_MODEL_NAME_DICT[pos] = 'pos00'
        self.modelTab_MODEL_BTN_DICT[pos].configure(
            state=tk.NORMAL, text=self.modelTab_MODEL_NAME_DICT[pos], command=self.modelTab_MODEL_FUN_DICT[pos])

    def model_function_update_01(self):
        pos = '01'

        def wrapper():
            messagebox.showinfo('提示', 'model01-update......')
        self.modelTab_MODEL_FUN_DICT[pos] = wrapper
        self.modelTab_MODEL_NAME_DICT[pos] = 'pos01'
        self.modelTab_MODEL_BTN_DICT[pos].configure(
            state=tk.NORMAL, text=self.modelTab_MODEL_NAME_DICT[pos], command=self.modelTab_MODEL_FUN_DICT[pos])

    def model_function_update_40(self):
        pos = '40'

        def wrapper():
            from ...zuoyianmo.c62x_zuoyianmo import SeatTest
            SeatTest(masterUI=self.masterUI,
                     parentWin=self.masterUI.can_unitTab)
        self.modelTab_MODEL_FUN_DICT[pos] = wrapper
        self.modelTab_MODEL_NAME_DICT[pos] = 'C62X座椅按摩'
        self.modelTab_MODEL_BTN_DICT[pos].configure(
            state=tk.NORMAL, text=self.modelTab_MODEL_NAME_DICT[pos], command=self.modelTab_MODEL_FUN_DICT[pos])

    def model_function_update_50(self):
        pos = '50'

        def wrapper():
            from ...helpdoc.helpdoc import HelpdocWindow
            HelpdocWindow(masterUI=self.masterUI, parentWin=self)

        self.modelTab_MODEL_FUN_DICT[pos] = wrapper
        self.modelTab_MODEL_NAME_DICT[pos] = '帮助文档'
        self.modelTab_MODEL_BTN_DICT[pos].configure(
            state=tk.NORMAL, text=self.modelTab_MODEL_NAME_DICT[pos], command=self.modelTab_MODEL_FUN_DICT[pos])

    def modelTabViewInit(self):
        for i, j in self.modelTab_MODEL_POS_LIST:
            self.modelTab_MODEL_BTN_DICT[f"{i}{j}"] = tk.Button(
                self, text=self.modelTab_MODEL_NAME_DICT[f'{i}{j}'], state=tk.DISABLED, padx=10, pady=5, command=self.modelTab_MODEL_FUN_DICT[f'{i}{j}'](i, j))
            self.modelTab_MODEL_BTN_DICT[f"{i}{j}"].grid(row=i, column=j)

        # DOC 调整行列最小间距
        col_count, row_count = self.grid_size()
        for col in range(col_count):
            self.grid_columnconfigure(col, minsize=110)
        for row in range(row_count):
            self.grid_rowconfigure(row, minsize=60)

    def autoUnitTabInit(self):
        self.autoUnitTabDataInit()
        self.autoUnitTabViewInit()
        self.autoUnitTab_btn_function_update()
        pass
